package com.sl.au.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.sl.au.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.repository.MaintainSchemeRepository;
import com.sl.au.service.AfterSaleMaintainService;
import com.sl.au.service.CustomerService;
import com.sl.au.service.OptionService;
import com.sl.au.service.SaleAreaService;


/**
 * 售后设备维护方案
 */

@RestController
@RequestMapping(value = "/ui/afterSaleMaintain")
public class AfterSaleMaintainController extends BaseController {

	@Autowired
	CustomerService customerService;
	
	@Autowired
	SaleAreaService saleAreaService;
	
	@Autowired
	OptionService  optionService;
	
	@Autowired
	AfterSaleMaintainService  afterSaleMaintainService;
	
	@Autowired
	MaintainSchemeRepository maintainSchemeRepository;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("afterSaleMaintain/maintainindex");
		return modelAndView;
	}

	/**
	 * 获得当前类型的维护方案
	 */
	@RequestMapping(value="/getpage",method = RequestMethod.GET)
	public Map<String, Object> getpage(HttpServletRequest request){
		Map<String, Object> map=new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String deviceID=request.getParameter("deviceID");
		String key1=request.getParameter("key");
		String key = deviceID;
		Long count = (long) 0;
		List<MaintainScheme> maintainSchemes=new ArrayList<>();
		if(key.equals("all")){
			if(org.springframework.util.StringUtils.hasText(key1)) {
				maintainSchemes = afterSaleMaintainService.getSchemes(start, length, key1, null, null);
				count = afterSaleMaintainService.getRepository().getSchemesCount(key1);
			}
			else {
				maintainSchemes = afterSaleMaintainService.getAllSchemes(start, length, null, null, null);
				count = afterSaleMaintainService.getRepository().getAllSchemesCount();
			}
		}
		else {
			key=deviceID;
			maintainSchemes = afterSaleMaintainService.getMaintainScheme(start, length, key, null, null);
			count = afterSaleMaintainService.getRepository().getCount(key);
		}
		map.put("data", maintainSchemes);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	/**
	 * /得到树节点数据
	 */

	@RequestMapping(value = "/getTree", method = RequestMethod.GET)
	public Map<String, Object> getTree() {
		Map<String, Object> map = new HashMap<String, Object>();
		String deviceType="产品类型";
		List<Option> options=new ArrayList<>();
		options=optionService.getRepository().findByGroup(deviceType);
		List<Map<String,Object>> deviceTypeMap=new ArrayList<Map<String,Object>>();
		deviceTypeMap=afterSaleMaintainService.getNodeMap(options);
		map.put("code", 1);
		map.put("deviceType", deviceTypeMap);
		return map;
	}
	/**
	 * /获得当前维护方案的维护项目/
	 */
	@RequestMapping(value="/getItem",method = RequestMethod.GET)
	public Map<String, Object> getItem(HttpServletRequest request){
		Map<String, Object> map=new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String schemeID=request.getParameter("schemeID");
		String key = null;
		Long count = (long) 0;
		if (StringUtils.hasText(schemeID)){
			key = schemeID;
			count = afterSaleMaintainService.getRepository().getItemCount(key);
		}
		List<MaintainItem> maintainItems = afterSaleMaintainService.getMaintainItem(start, length, key, null, null);
		map.put("data", maintainItems);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	/**
	 *打开新增页面
	 */

	@RequestMapping(value = "/add/{type}/{deviceID}/{deviceType}", method = RequestMethod.GET)
	public ModelAndView addScheme(@PathVariable String type,
								 @PathVariable String deviceID,
								 @PathVariable String deviceType,
								 HttpServletRequest request) {
		ModelAndView mv = createLayoutView("afterSaleMaintain/maintainAdd");
		
		mv.addObject("type", type);
		mv.addObject("deviceID", deviceID);
		mv.addObject("deviceType", deviceType);
		return mv;
	}

	/**
	 * 打开编辑页面
	 */

	@RequestMapping(value = "/editScheme/{id}", method = RequestMethod.GET)
	public ModelAndView editScheme(@PathVariable String id, HttpServletRequest request) {
		ModelAndView mv = createLayoutView("afterSaleMaintain/maintainEdit");
		MaintainScheme ms= afterSaleMaintainService.getRepository().findOne(id);
		mv.addObject("schemeID", id);
		mv.addObject("type", ms.getType());
		mv.addObject("deviceID", ms.getDeviceID());
		mv.addObject("deviceType", ms.getDeviceType());
		mv.addObject("schemeName", ms.getSchemeName());
		mv.addObject("schemeCode", ms.getSchemeCode());
		return mv;
	}

	/**
	 * 保存维护方案，返回方案ID
	 */
	@RequestMapping(value = "/saveScheme", method = RequestMethod.POST)
	public Map<String, Object> save(HttpServletRequest request,@RequestBody MaintainScheme maintainScheme){
		Map<String, Object> map = new HashMap<String, Object>();
		afterSaleMaintainService.getRepository().save(maintainScheme);
		String schemeId=maintainScheme.getId();
		map.put("code", 0);
		map.put("schemeId", schemeId);
		return map;	
	}

	/**
	 * /删除维护方案
	 */
	@RequestMapping(value = "/delScheme", method = RequestMethod.GET)
	public Map<String, Object> delScheme(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = request.getParameter("ids");
		String id[] = ids.split(",");
		List<MaintainScheme> schemes = new ArrayList<MaintainScheme>();
		for (int i = 0; i < id.length; i++) {
			String schemeId = id[i];
			MaintainScheme scheme = new MaintainScheme();
			scheme.setId(schemeId);
			schemes.add(scheme);
			///// 删除与维护方案相关的维护项目
			maintainSchemeRepository.deleteItemBySchemeId(schemeId);
		}
		afterSaleMaintainService.getRepository().deleteInBatch(schemes);
		map.put("code", 0);
		return map;
	}

	/**
	 * /删除维护项目
	 */
	@RequestMapping(value = "/delItem", method = RequestMethod.GET)
	public Map<String, Object> delItem(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = request.getParameter("ids");
		String id[] = ids.split(",");
		List<MaintainItem> items = new ArrayList<MaintainItem>();
		for (int i = 0; i < id.length; i++) {
			String itemId = id[i];
			MaintainItem item = new MaintainItem();
			item.setId(itemId);
			items.add(item);
		}
		afterSaleMaintainService.getRepository1().deleteInBatch(items);
		map.put("code", 0);
		return map;
	}
	
	/**
	 * 数据提交前验证方案编号是否唯一
	 */
	@RequestMapping(value = "/validSchemeCode/{schemeID}/{schemeCode}", method = RequestMethod.GET)
	public Map<String, Object> validSchemeCode(@PathVariable String schemeID, 
								   @PathVariable String schemeCode, 
								   HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String state = "0";
		if (!StringUtils.hasText(schemeID)){
			schemeID = "0";
		}
		//得到方案编号为schemeCode的方案
		List<MaintainScheme> mss = new ArrayList<MaintainScheme>();
		if (StringUtils.hasText(schemeCode)){
			mss = afterSaleMaintainService.getRepository().getSchemeByCode(schemeCode);
		}
		if (mss.size()>1){
			state = "1";
		}else if (mss.size()==1){
			if (!mss.get(0).getId().equals(schemeID)){
				state = "1";
			}
		}
		map.put("state", state);
		return map;
	}
	
}
